package io.jsen.meta.diff.obj.abs;

import io.jsen.meta.diff.field.MetaField;
import io.jsen.meta.diff.obj.MetaObject;

import java.util.Collection;

import static io.jsen.meta.diff.support.MetaChangeModifier.*;

/**
 * @author jsen
 * @date 2021/05/01
 **/
public abstract class AbstractComplex<O extends AbstractComplex<O, S>, S> extends MetaObject<O, S> {

    protected AbstractComplex(MetaField field, S val) {
        super(field, val);
    }

    protected int calCollectionState(Collection<MetaObject<?, ?>> collection) {
        if (collection.stream().allMatch(v -> isDelete(v.getModifier()))) {
            return setDelete(0);
        } else if (collection.stream().allMatch(v -> isInsert(v.getModifier()))) {
            return setInsert(0);
        } else if (collection.stream().anyMatch(v -> isChange(v.getModifier()) ||
                isInsert(v.getModifier()) || isDelete(v.getModifier()))) {
            return setChange(0);
        } else {
            return setNoChange(0);
        }
    }
}
